package middleware.zookeeper;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

/**
 * zookeeper 工具类：
 *
 * @author ME
 * @date 2021/5/12
 */
public class ZkUtil {

    public static String create(ZooKeeper zkClient, String path, String value,
                                CreateMode createMode) {
        try {
            return zkClient.create(path, value.getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String create(ZooKeeper zkClient, String path, String value,
                                List<ACL> acl, CreateMode createMode) {
        try {
            return zkClient.create(path, value.getBytes(StandardCharsets.UTF_8), acl, createMode);
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String getData(ZooKeeper zkClient, String path) {
        try {
            byte[] bn = zkClient.getData(path, false, null);
            return new String(bn, StandardCharsets.UTF_8);
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String getData(ZooKeeper zkClient, String path, Stat stat) {
        try {
            byte[] bn = zkClient.getData(path, false, stat);
            return new String(bn, StandardCharsets.UTF_8);
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static void delete(ZooKeeper zkClient, String path) {
        try {
            zkClient.delete(path, -1);
        } catch (InterruptedException | KeeperException e) {
            e.printStackTrace();
        }
    }

    public static void delete(ZooKeeper zkClient, String path, int version) {
        try {
            zkClient.delete(path, version);
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static List<String> getChild(ZooKeeper zkClient, String path) {
        try {
            return zkClient.getChildren(path, false);
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    public static List<String> getChild(ZooKeeper zkClient, String path, Stat stat) {
        try {
            return zkClient.getChildren(path, false, stat);
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

}
